package com.wxq.yaoxia.util;

import java.io.File;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import android.util.Log;

public class YXDBHelper {
	private final Context context;
	private SQLiteDatabase db;
	private final YXSQLiteOpenHelper dbOpenHelper;
	public YXDBHelper(Context context) {
		super();
		this.context = context;
		dbOpenHelper = new YXSQLiteOpenHelper(
				this.context, 
				YXConstants.DATABASE_NAME, 
				null, 
				YXConstants.DATABASE_VERSION);
	}
	public void close() {
		if(db.isOpen())
		{
			db.close();
		}	
	}
	
	public void open() {
		try {
			db = dbOpenHelper.getReadableDatabase();
		} catch (SQLiteException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	//-----------获得建议------------------------------------------
	public Cursor getDrugSuggestion(String drug) {
		String sql = YXConstants.QUERY_DRUG_SUGGEST;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		//Log.d("wxq", "sql="+sql);
		String args[] = {drug+"%"};
		Cursor cursor = db.rawQuery(sql, args);
		return cursor;
	}
	
	public Cursor getDiseaseSuggestion(String disease) {
		String sql = YXConstants.QUERY_DISEASE_SUGGEST;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		//Log.d("wxq", "sql="+sql);
		String args[] = {disease+"%"};
		Cursor cursor = db.rawQuery(sql, args);
		return cursor;
	}
	
	public Cursor getBothSuggestion(String queryString) {
		String sql = YXConstants.QUERY_BOTH_SUGGEST;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		//Log.d("wxq", "sql="+sql);
		String args[] = {queryString+"%",queryString+"%"};
		Cursor cursor = db.rawQuery(sql, args);
		return cursor;
	}
	
	//-----------获得药品查询------------------------------------------
	public Cursor getDrugsWithName(String name) {
		String sql = YXConstants.QUERY_DRUGS_WITH_NAME;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {name+"%"};
		return db.rawQuery(sql, args);
	}
	
	public Cursor getDrugWithID(String Id) {
		String sql = YXConstants.QUERY_DRUG_WITH_ID;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {Id};
		return db.rawQuery(sql, args);
	}
	
	public Cursor getDrugsWithQueryId(String queryId) {
		String sql = YXConstants.QUERY_DRUG_WITH_QUERYID;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {queryId};
		return db.rawQuery(sql, args);
	}
	
	public Cursor getDrugsCommonUse(String queryId) {
		String sql = YXConstants.QUERY_DRUG_COMMON_USE;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		return db.rawQuery(sql, null);
	}
	
	//-----------获得疾病查询------------------------------------------
	public Cursor getDiseaseWithName(String name) {
		String sql = YXConstants.QUERY_DISEASE_WITH_NAME;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {name+"%"};
		return db.rawQuery(sql, args);
	}
	
	public Cursor getDiseaseWithID(String Id) {
		String sql = YXConstants.QUERY_DISEASE_WITH_ID;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {Id};
		return db.rawQuery(sql, args);
	}
	
	public Cursor getDiseaseWithQueryId(String queryId) {
		String sql = YXConstants.QUERY_DISEASE_WITH_QUERYID;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {queryId};
		return db.rawQuery(sql, args);
	}
	
	public Cursor getDiseaseCommonUse(String queryId) {
		String sql = YXConstants.QUERY_DISEASE_COMMON_USE;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		return db.rawQuery(sql, null);
	}
	
	//-----------获得所有查询------------------------------------------
	
	public Cursor getBothWithName(String name) {
		String sql = YXConstants.QUERY_BOTH_WITH_NAME;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {name+"%",name+"%"};
		return db.rawQuery(sql, args);
	}
	
	public Cursor getBothWithDiseaseID(String id) {
		String sql = YXConstants.QUERY_BOTH_WITH_DISEASE_ID;
		if(db == null || !db.isOpen())
		{
			open();	
		}
		String args[] = {id};
		return db.rawQuery(sql, args);
	}
	
	//-----------装载数据库------------------------------------------
	public static void setupDatabase(Context context) {
		String fileFullName = YXConstants.DATABASE_PATH+YXConstants.DATABASE_NAME;
		File dbFile = new File(fileFullName);
		if(!dbFile.exists())
		{
			// 创建目标文件
		    YXUtility.CreateFile(fileFullName);
			YXUtility.copyFileFromAssets(context, YXConstants.DATABASE_NAME, dbFile);
			Log.d("wxq", "setup database");
		}
		else {
			Log.d("wxq", "database already existed!");
		}
	}
	
//	//检查并安装更新文件
//	public static void setupDBUpdateFile(Context context) {
//		String fileFullName = Environment.getExternalStorageDirectory()+YXConstants.DBUpdateFile_PATH+YXConstants.UPDATE_FILE_NAME;
//		File dbFile = new File(fileFullName);
//		if(!dbFile.exists())
//		{
//			// 创建目标文件
//		    YXUtility.CreateFile(fileFullName);
//			YXUtility.copyFileFromAssets(context, YXConstants.UPDATE_FILE_NAME, dbFile);
//			Log.d("wxq", "setup update file");
//		}
//		else {
//			Log.d("wxq", "update file already existed!");
//		}
//	}
//
//	//获得本地版本号
//	public static int getLocalVersion() {
//		String fileFullName = Environment.getExternalStorageDirectory()+YXConstants.DBUpdateFile_PATH+YXConstants.UPDATE_FILE_NAME;
//		File file = new File(fileFullName);
//		if(!file.exists())
//		{
//			Log.d("wxq", "conf file does NOT exists!");
//			return 0;
//		}
//		FileInputStream fis = null;
//		try {
//			fis = new FileInputStream(file);
//			
//		} catch (FileNotFoundException e) {
//			 
//			e.printStackTrace();
//		}
//		
//		return getLocalVersion(fis);
//	}
//    public static int getLocalVersion(InputStream inputStream) {
//    	int version = 0;
//    	if(inputStream == null)
//    	{
//    		Log.d("wxq", "inputStream is null");
//    		return version;
//    	}
//    	try {
//        	XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//            factory.setNamespaceAware(true);
//            XmlPullParser parser = factory.newPullParser();
//            parser.setInput(inputStream, null);
//			parser.next();
//			int eventType = parser.getEventType();
//			String TagName = "";
//			String TagValue = "";
//			while (eventType != XmlPullParser.END_DOCUMENT)
//			{
//				if(eventType == XmlPullParser.START_DOCUMENT)
//				{
//				   
//				}
//				else if(eventType == XmlPullParser.START_TAG)
//				{
//				 
//					TagName = parser.getName();
//				}
//				else if(eventType == XmlPullParser.END_TAG)
//				{
//				 
//					TagName = parser.getName();
//					if(TagName.equals("version"))
//					{
//						Log.d("wxq", "version="+TagValue);
//						version = Integer.parseInt(TagValue);
//					}
//					
//				}
//				else if(eventType == XmlPullParser.TEXT)
//				{
//				
//					TagValue = parser.getText();
//				}
//				eventType = parser.next();
//			}//eof-while
//			if(inputStream!=null)
//			{
//				inputStream.close();
//			}
//			
//		} catch (XmlPullParserException e) {
//
//			e.printStackTrace();
//		} catch (IOException e) {
//
//			e.printStackTrace();
//		}
//    	
//    	return version;
//	}

}
